import {
    BQGIS_video
} from './BQGIS-video'

import {
    globalLayer,
    globalTool
} from '../BQGIS-globalVar'

import {
    BQGIS_initRoamingByKey
} from "../BQGIS-roamingKey"

export function initPolygon(viewer = window.viewer, callback = {}) {
    if (!globalLayer.videoPolygonLayer) {
        globalLayer.videoPolygonLayer = new mars3d.layer.GraphicLayer()
        viewer.addLayer(globalLayer.videoPolygonLayer)
    }
    if (!globalTool.videoPolygonDom) {
        globalTool.videoPolygonDom = new BQGIS_video()
    }
}

// 绘制地表图斑
export function drawPolygon(url) {
    const id = getUUId()
    const videoElement = globalTool.videoPolygonDom.createVideoElement(id)
    if (!videoElement) return
    if (url) {
        globalTool.videoPolygonDom.setVideoSrc({
            src: url,
            videoElement
        })
    }
    // 构造投射体
    return globalLayer.videoPolygonLayer.startDraw({
        id,
        type: "polygon",
        styleType: "video",
        style: {
            material: videoElement,
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            clampToGround: true
        }
    })
}

// 加载视频幕布
export function addPolygon(option) {
    if(!option || !option.positions) return
    const videoElement = globalTool.videoPolygonDom.createVideoElement(option.id)
    if (!videoElement) return
    globalTool.videoPolygonDom.setVideoSrc({
        src: option.url,
        videoElement
    })
    if(option.positions instanceof Array){ // 经纬度数组
        console.log(option.positions)
    }
    const graphic = new mars3d.graphic.PolygonEntity({
        id: option.id,
        name: option.name,
        positions: option.positions,
        style: {
            material: videoElement,
            clampToGround: true,
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            stRotationDegree: option.stRotationDegree || 0
        },
    })
    globalLayer.videoPolygonLayer.addGraphic(graphic)
    return graphic
}

export function removeVideoPolygon(graphic) {
    if (!graphic) return
    globalTool.videoPolygonDom.removeVideoElement(graphic.id)
    globalLayer.videoPolygonLayer.removeGraphic(graphic)
}

export function removeVideoPolygonById(id) {
    const graphic = getGraphicById(id)
    if (!graphic) return
    globalTool.videoPolygonDom.removeVideoElement(id)
    globalLayer.videoPolygonLayer.removeGraphic(graphic)
}


export function redrawPolygon(graphic){
    const videoElement =  graphic.style.material;
    const stRotationDegree = graphic.style.stRotationDegree
    globalLayer.videoPolygonLayer.removeGraphic(graphic)
    return globalLayer.videoPolygonLayer.startDraw({
        id: graphic.id,
        type: "polygon",
        styleType: "video",
        style: {
            material: videoElement,
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            clampToGround: true,
            stRotationDegree
        }
    })
}

export function flyTo(graphic){
    graphic.flyTo()
}

export function getGraphicById(id) {
    if (globalLayer.videoPolygonLayer) {
        return globalLayer.videoPolygonLayer.getGraphicById(id)
    }
}
function getUUId() {
    let tem = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    let id = "",
        len = tem.length,
        i = 0
    for (i; i < len; i++) {
        if (tem[i] == "-") id += tem[i]
        else {
            let num = Math.floor(Math.random() * 16)
            id += num.toString(16)
        }
    }
    return id
}
